home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / TIMTST.C < prev    next >
C/C++ Source or Header  |  1993-05-26  |  4KB  |  103 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 05-24-93 (00:35)             Number: 87
  4. From: BOB STOUT                    Refer#: 226
  5.   To: BRIAN DESSENT                 Recvd: NO  
  6. Subj: Fastest way to check exis      Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. Brian...
  9.  
  10.   I investigated this a bit further and thought you might appreciate seeing the
  11. results. Following is test code using several different methods for both existin
  12. g and non-existing files (since this was a quick hack, excuse the compiler speci
  13. ficities and MFL-isms). The timings follow.
  14.  
  15. ----8<------------------------------------------------------------------------
  16. /*
  17. **  timtst.c
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <io.h>
  23. #include <fcntl.h>
  24. #include <sys\stat.h>
  25. #include <sys\types.h>
  26. #include <dos.h>
  27. #include <mfltime.h>
  28.  
  29. char *names[] = {
  30.       "timtst.c",             /* This file (i.e. it exists) */
  31.       "abc.xyz",              /* Bogus noexistant file      */
  32.       NULL
  33.       };
  34.  
  35. int main(void)
  36. {
  37.       char **p;
  38.       uclock_t start, end;
  39.       int fp, retval;
  40.       FILE *fd;
  41.       struct find_t ff;
  42.       union REGS regs;
  43.  
  44.       for (p = names; *p; ++p, puts(""))
  45.       {
  46.             start = usec_clock();
  47.             fp = open(*p, O_RDONLY);
  48.             end = usec_clock();
  49.             close(fp);
  50.             printf("open(%s) returned %d and took %d.%d msec.\n", *p, fp,
  51.                   (int)(usec_difftime(start, end) / 1000L),
  52.                   (int)(usec_difftime(start, end) % 1000L));
  53.  
  54.             start = usec_clock();
  55.             fd = fopen(*p, "r");
  56.             end = usec_clock();
  57.             fclose(fd);
  58.             printf("fopen(%s) returned %p and took %d.%d msec.\n", *p, fd,
  59.                   (int)(usec_difftime(start, end) / 1000L),
  60.                   (int)(usec_difftime(start, end) % 1000L));
  61.  
  62.             start = usec_clock();
  63.             retval = _dos_findfirst(*p, 0xff, &ff);
  64.             end = usec_clock();
  65.             printf("findfirst(%s) returned %d and took %d.%d msec.\n", *p,
  66.                   retval, (int)(usec_difftime(start, end) / 1000L),
  67.                   (int)(usec_difftime(start, end) % 1000L));
  68.  
  69.             regs.x.ax = 0x4300;
  70.             regs.x.dx = (unsigned)(*p);
  71.             start = usec_clock();
  72.             intdos(®s, ®s);
  73.             end = usec_clock();
  74.             printf("DOS function 0x43(%s) returned %d and took %d.%d msec.\n",
  75.                   *p, regs.x.cflag, (int)(usec_difftime(start, end) / 1000L),
  76.                   (int)(usec_difftime(start, end) % 1000L));
  77.       }
  78. }
  79. ----8<------------------------------------------------------------------------
  80. open(timtst.c) returned 5 and took 16.584 msec.
  81. fopen(timtst.c) returned 035A and took 16.939 msec.
  82. findfirst(timtst.c) returned 0 and took 8.47 msec.
  83. DOS function 0x43(timtst.c) returned 0 and took 8.16 msec.
  84.  
  85. open(abc.xyz) returned -1 and took 8.385 msec.
  86. fopen(abc.xyz) returned 0000 and took 8.627 msec.
  87. findfirst(abc.xyz) returned 18 and took 8.196 msec.
  88. DOS function 0x43(abc.xyz) returned -1 and took 8.5 msec.
  89. ----8<------------------------------------------------------------------------
  90.  
  91.   This suggests using find first or DOS function 0x43 rather than trying to open
  92.  the file although everything's comparable when the file doesn't exist. For refe
  93. rence, this was run on a 10 MHz 286 under DOS 3.3, compiled in tiny model with Z
  94. TC++ 3.1. Given the difference in compiler libraries, DOS function 0x43 might be
  95.  your best bet.
  96.  
  97.  
  98. --- QM v1.00
  99.  * Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
  100. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  101. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 261/1023
  102. SEEN-BY: 280/1 390/1 396/1 5 15 2270/1 2440/5 3603/20
  103.